/**
 * 菜单
 * */
import { includeArray, flattenSiderMenu } from '@/libs/system';

function buildParentList(arr,outList) {
    arr.forEach(g => {
        if (g.parentId != undefined) {
            let oid = g['id']
            //id：包含id的对象
            outList[oid] = g
        }
        if (g.children != undefined && (g.children || []).length > 0)
            buildParentList(g['children'],outList)
    })
}
export default {
    namespaced: true,
    state: {
        // 顶部菜单
        header: [],
        // 侧栏菜单
        sider: [],
        // 当前顶栏菜单的 name
        headerName: '',
        // 当前所在菜单的 path
        activePath: '',
        // 展开的子菜单 name 集合
        openNames: [],
        //菜单父节点列表
        parentList:{},
        //平级菜单
        flattenMenu:[]
    },
    getters: {
        /**
         * @description 根据 user 里登录用户权限，对顶栏菜单进行鉴权过滤
         * */
        filterHeader (state, getters, rootState) {
            const userInfo = rootState.admin.user.info;
            // @权限
            const access = userInfo.access;
            if (access && access.length) {
                return state.header.filter(item => {
                    let state = true;
                    if (item.auth && !includeArray(item.auth, access)) state = false;

                    return state;
                });
            } else {
                return state.header.filter(item => {
                    let state = true;
                    if (item.auth && item.auth.length) state = false;
                    return state;
                });
            }
        },
        /**
         * @description 当前 header 的全部信息
         * */
        currentHeader (state) {
            return state.header.find(item => item.name === state.headerName);
        },
        /**
         * @description 在当前 header 下，是否隐藏 sider（及折叠按钮）
         * */
        hideSider (state, getters) {
            let visible = false;
            if (getters.currentHeader && 'hideSider' in getters.currentHeader) visible = getters.currentHeader.hideSider;
            return visible;
        },
        /**
         * @description 根据当前的path获取菜单的id
         * */
        currentSiderMenuId: (state) => (path) => {
            for(let pro in state.parentList){
                if(state.parentList[pro] && state.parentList[pro].path == path){
                    return pro;
                }
            }
        },
    },
    mutations: {
        /**
         * @description 设置侧边栏菜单
         * @param {Object} state vuex state
         * @param {Array} menu menu
         */
        setSider (state, menu) {
            const outList = {};
            //将菜单转为平级
            state.flattenMenu = flattenSiderMenu(menu, []);
            state.sider = menu;
            buildParentList(menu, outList);
            state.parentList = outList;
        },
        /**
         * @description 设置顶栏菜单
         * @param {Object} state vuex state
         * @param {Array} menu menu
         */
        setHeader (state, menu) {
            state.header = menu;
        },
        /**
         * @description 设置当前顶栏菜单 name
         * @param {Object} state vuex state
         * @param {Array} name headerName
         */
        setHeaderName (state, name) {
            state.headerName = name;
        },
        /**
         * @description 设置当前所在菜单的 path，用于侧栏菜单高亮当前项
         * @param {Object} state vuex state
         * @param {Array} path fullPath
         */
        setActivePath (state, path) {
            state.activePath = path;
        },
        /**
         * @description 设置当前所在菜单的全部展开父菜单的 names 集合
         * @param {Object} state vuex state
         * @param {Array} names openNames
         */
        setOpenNames (state, names) {
            state.openNames = names;
        },
        /**
         * @description 清空所有信息
         * @param {Object} state vuex state
         */
        clear(state){
            state.flattenMenu = [];
        }
    }
}
